package com.dd.logistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.logistics.common.Result;
import com.dd.logistics.entity.InboundOrder;
import com.dd.logistics.service.InboundOrderService;
import com.dd.logistics.utils.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/inbound")
public class InboundOrderController {
    
    @Autowired
    private InboundOrderService inboundOrderService;
    
    @GetMapping("/list")
    public Result<List<InboundOrder>> list() {
        try {
            List<InboundOrder> list = inboundOrderService.getInboundOrderList();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取入库单列表失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/page")
    public Result<Page<InboundOrder>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<InboundOrder> page = inboundOrderService.getInboundOrderPage(current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("分页查询入库单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/create")
    public Result<Boolean> createInboundOrder(@RequestBody InboundOrder inboundOrder) {
        try {
            if (inboundOrder.getSupplierId() == null || inboundOrder.getProductCode() == null) {
                return Result.error("供应商ID和商品编码不能为空");
            }
            if (inboundOrder.getQuantity() == null || inboundOrder.getQuantity() <= 0) {
                return Result.error("入库数量必须大于0");
            }
            
            // 自动生成入库单号
            inboundOrder.setOrderNumber(OrderNumberGenerator.generateInboundNumber());
            // 设置初始状态
            inboundOrder.setStatus(InboundOrder.STATUS_PENDING);
            
            boolean success = inboundOrderService.createInboundOrder(inboundOrder);
            return success ? Result.success("创建入库单成功", true) : Result.error("创建入库单失败");
        } catch (Exception e) {
            return Result.error("创建入库单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/confirm/{orderNumber}")
    public Result<Boolean> confirmInbound(@PathVariable String orderNumber) {
        try {
            if (orderNumber == null || orderNumber.trim().isEmpty()) {
                return Result.error("入库单号不能为空");
            }
            
            boolean success = inboundOrderService.confirmInbound(orderNumber.trim());
            return success ? Result.success("确认入库成功", true) : Result.error("确认入库失败");
        } catch (Exception e) {
            e.printStackTrace(); // 打印详细错误日志
            return Result.error("确认入库失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/detail/{orderNumber}")
    public Result<InboundOrder> getDetail(@PathVariable String orderNumber) {
        try {
            QueryWrapper<InboundOrder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_number", orderNumber);
            InboundOrder order = inboundOrderService.getOne(wrapper);
            
            if (order == null) {
                return Result.error("入库单不存在");
            }
            return Result.success(order);
        } catch (Exception e) {
            return Result.error("获取入库单详情失败：" + e.getMessage());
        }
    }
} 